//
//  RGCardCollectionViewCell.m
//  atlas
//
//  Created by 你好 on 17/1/3.
//  Copyright © 2017年 xyxNav. All rights reserved.
//

#import "RGCardCollectionViewCell.h"

@implementation RGCardCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        [self configUI];
    }
    return self;
}



-(void)configUI
{
    self.contentImageView =[UIImageView new];
//    self.contentImageView.contentMode=UIViewContentModeScaleAspectFill;
    self.contentImageView.clipsToBounds=YES;
    self.contentImageView.userInteractionEnabled=YES;
    [self.contentView addSubview:self.contentImageView];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentView);
    }];

    self.coverView=[UIView new];
    self.coverView.backgroundColor=[UIColor colorWithWhite:1 alpha:0.5];
    [self.contentImageView addSubview:self.coverView];
    
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentImageView);
    }];
    
    self.tapView=[UIView new];
    self.tapView.backgroundColor=[UIColor clearColor];
    [self.contentImageView addSubview:self.tapView];
    [self.tapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentImageView);
    }];
    
    UITapGestureRecognizer *doubleClickTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleClickTapGesture:)];
    doubleClickTap.numberOfTapsRequired=2;
    doubleClickTap.numberOfTouchesRequired=1;
    [self.tapView addGestureRecognizer:doubleClickTap];
   
}


-(void)doubleClickTapGesture:(UITapGestureRecognizer *)tapGesture
{
    if (_delegate && [_delegate respondsToSelector:@selector(doubleClickCell:)]) {
        
        [_delegate doubleClickCell:self];
        
    }
}


-(void)configRecommendData:(Recommend *)recommend
{
    self.coverView.alpha=1.0f;
    
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@?token=%@",GETIMAGE_URL,recommend.imageUrl,[UserManager shareInstance].userToken]];
    
    [self.contentImageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        UIView *tempView=[[UIView alloc]init];
        tempView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:tempView];
        
        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self.contentView);
        }];
        
        [UIView animateWithDuration:1.5 animations:^{
            self.coverView.alpha=0.0f;
        }completion:^(BOOL finished) {
            [tempView removeFromSuperview];
        }];
    }];
    
}


-(void)configPhotoData:(Photo *)photo
{
    self.coverView.alpha=1.0f;
    
    NSURL *url=nil;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@?token=%@",GETIMAGE_URL,photo.imageUrl,[UserManager shareInstance].userToken]];
    }
    else
    {
        url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@?token=%@",GETIMAGE_URL,photo.tabletUrl,[UserManager shareInstance].userToken]];
    }
   
    [self.contentImageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        UIView *tempView=[[UIView alloc]init];
        tempView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:tempView];
        
        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self.contentView);
        }];
        
        [UIView animateWithDuration:1.5 animations:^{
            self.coverView.alpha=0.0f;
        }completion:^(BOOL finished) {
            [tempView removeFromSuperview];
        }];
    }];
}

@end
